package com.fruits.controller;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;

import com.fruits.constant.FruitsConstant;
import com.jfinal.core.Controller;
import com.wx.exception.WxException;
import com.wx.util.WxMpUtil;

/**   
* @Title: WxCustomerController.java 
* @Package com.fruits.controller 
* @Description: TODO(用一句话描述该文件做什么) 
* @author chxl   
* @date 2015年8月19日 下午10:14:04 
* @version V1.0   
*/
public class WxCustomerController extends Controller {
	/**
	 * @throws WxErrorException 
	 * @throws WxException 
	 * 
	 */
	public void index() throws WxException, WxErrorException{
		WxMpUser wxMpUser = authFromMp();
		//得到附近店铺
		
		//得到附近热门水果
		
		super.renderFreeMarker("/mobile/wxcustomer/wxcustomer_index.ftl");
	}
	
	private WxMpUser authFromMp() throws WxException, WxErrorException {
		if (getSession().getAttribute(FruitsConstant.WX_USER) == null) {
			String appName = getPara("appName");
			String code = getPara("code");
			WxMpService wxMpService = WxMpUtil.getWxMpServiceByAppName(appName);
			WxMpUser wxMpUser = wxMpService.userInfo("oWxChswR0x2ksijzyKUFo2nGhwko", "zh_CN");

			getSession().setAttribute(FruitsConstant.WX_USER, wxMpUser);
			getSession().setAttribute(FruitsConstant.APPNAME, appName);
		}
		return (WxMpUser) getSession().getAttribute(FruitsConstant.WX_USER);
	}
	
	
}
